package cn.sanshuilin.kst.system.provider.serviceimpl;

import cn.sanshuilin.kst.common.api.OperationPlatform;
import cn.sanshuilin.kst.common.utils.DateUtil;
import cn.sanshuilin.kst.common.utils.LogPrinter;
import cn.sanshuilin.kst.common.utils.StringUtil;
import cn.sanshuilin.kst.system.api.service.IMemberService;
import cn.sanshuilin.kst.system.provider.bean.LoginRecordBean;
import cn.sanshuilin.kst.system.provider.bean.Member;
import cn.sanshuilin.kst.system.provider.write.dao.LoginRecordDao;
import cn.sanshuilin.kst.system.provider.write.dao.MemberDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author greenlee
 * @version v1.0
 * @create 创建时间: 2018-01-29 20:21
 * Copyright 2017  by 三水林科技有限公司
 **/
@Service("memberService")
public class MemberService implements IMemberService {

    @Autowired
    private MemberDao memberDao;
    @Autowired
    private LoginRecordDao loginRecordDao;

    public void saveMember(Member member) {
        try {
//            if (StringUtils.isEmpty(member.getId())) {
            member.setRegistDate(DateUtil.getNowDate());
            member.setFanCount(0);
            member.setAttentionCount(0);
            member.setInformationCount(0);
            member.setShareCount(0);
            member.setAttitudeCount(0);
            member.setCommentCount(0);
            member.setStatusId(CDict.MemberStatusNormal.getId());
            member.setGroupId(CDict.MemberGroupPartner.getId());
            member.setStatusId(12);
            member.setGroupId(4);
//            }

            Member member1 = memberDao.saveAndFlush(member);
            LogPrinter.info("getPickMembersgetPickMembersmember" + member1);
        } catch (Exception e) {
            LogPrinter.error("saveMember" + e.getMessage());
        }
    }

    @Override
    public Member _getMemberByUsername(String strUsername) {
        Member member = memberDao.findByUsername(strUsername);
        return member;
    }

    @Override
    public Member login(String mobile, String password, OperationPlatform platform, String deviceId) {
        String md5Password = StringUtil.md5EncryptDouble(password);
        Member member = memberDao.findByMobileAndPassword(mobile, md5Password);
        if (null == member) {
            return null;
        }
        LoginRecordBean recordBean = addToken(mobile, member.getId(), deviceId, platform, 0 + "");
        member.setToken(recordBean.getToken());
        return member;
    }

    private LoginRecordBean addToken(String mobile, String accountId, String deviceId, OperationPlatform platform,
                                     String version) {
        // 旧的token作废
        if (platform.ordinal() == OperationPlatform.QTSHE_ANDROID_USER.ordinal()
                || platform.ordinal() == OperationPlatform.QTSHE_IOS_USER.ordinal()) {
            loginRecordDao.removeAccountId(accountId);

        }
        // 生成token
        String token = StringUtil.md5Encrypt(mobile + System.currentTimeMillis());
        LoginRecordBean recordPO = new LoginRecordBean();
        recordPO.setAccountId(accountId);
        recordPO.setToken(token);
        recordPO.setPlatform(platform.getChinese());
        recordPO.setDeviceId(deviceId);
        recordPO.setVersion(version);
        loginRecordDao.saveAndFlush(recordPO);
        return recordPO;
    }


}
